package com.czk.saatext2image.controller;

import com.alibaba.cloud.ai.dashscope.image.DashScopeImageOptions;
import jakarta.annotation.Resource;
import org.springframework.ai.image.ImageModel;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: chenzhangkun
 * @CreateTime: 2025-10-21
 * @Description:
 * @Version: 1.0
 */
@RestController
public class Text2ImageController {
    @Resource
    private ImageModel imageModel;
    /**
     * s生成图片的模型
     */
    private static final String IMAGE_MODEL = "wanx2.1-t2i-turbo";

    /**
     * 会生成图片url
     *
     * @param prompt
     * @return
     */
    @GetMapping(value = "/text2image")
    public String image(@RequestParam(name = "prompt", defaultValue = "熊猫") String prompt) {

        return imageModel.call(
                        new ImagePrompt(prompt, DashScopeImageOptions.builder().withN(1).withModel(IMAGE_MODEL).build())
                ).getResult()
                .getOutput()
                .getUrl();
    }
}
